<template>
  <div class="win" v-if="member_type">
    <div class="bj_box">
      <p>汽美养护</p>
    </div>
    <form @submit="submit" class="form_box">
      <div class="position_box">
        <p class="title_top">登录</p>
        <div class="input_box">
          <div class="input_img_box">
            <img v-if="bjurl" class="phone_logo" :src="bjurl+'login_phone_log.png'" />
            <input
              class="phone_input"
              type="number"
              v-model="phone"
              placeholder="请输入新手机号"
              maxlength="11"
              placeholder-style="color:#C3C3C3"
            />
          </div>
          <div class="input_img_box code_row">
            <div class="yzm_box">
              <img v-if="bjurl" class="code_logo" :src="bjurl+'login_code_logo.png'" />
              <input
                class="yzm"
                type="number"
                v-model="yzm"
                placeholder="请输入验证码"
                maxlength="6"
                placeholder-style="color:#C3C3C3"
              />
            </div>
            <button v-if="yzmShow!=true" class="grey">{{countdown+'s倒计时'}}</button>
            <button v-else @click="getYzm">获取验证码</button>
          </div>
        </div>
        <div class="btn_pos">
          <div class="btn_box">
            <button class="login_btn" form-type="submit">登录</button>
            <div class="btn_row" v-if="member_type!=1">
              <!-- <div class="btn_row"> -->
              <p>还没有申请过合作门店？</p>
              <button
                class="getPhone"
                open-type="getPhoneNumber"
                @getphonenumber="getPhoneNumber"
              >游客登录</button>
            </div>
          </div>
        </div>
      </div>
    </form>
    <i-toast id="toast" />
  </div>
  <div class="container" v-else>
    <div class="icon_load">
      <p>Loading...</p>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      nickName: "",
      member_type: "",
      bjurl: "",
      countdown: 60, //验证码倒计时
      yzmShow: true, //验证码按钮显示
      phone: "", //双向绑定手机号
      yzm: "", //双向绑定验证码
      encryptedData: "",
      iv: "",
      it: 0
    };
  },
  methods: {
    // 退出登录
    loginExit() {
      this.$ajax("/home/login/exitLogin", "post", {
        member_id: wx.getStorageSync("member_id")
      })
        .then(res => {
          if (res.status) {
          } else {
          }
        })
        .catch(err => {});
    },
    // 请求接口手机号登录
    loginIndex() {
      this.$ajax("/home/login/loginIndex", "post", {
        phone: this.phone,
        code: this.yzm,
        // open_id: wx.getStorageSync("open_id")
        open_id: "oUqga4_hvC21S3Ep34Q3aed_R3x4"
      })
        .then(res => {
          
          if (res.status) {
            wx.setStorageSync("member_id", res.data.member_id);
            wx.setStorageSync("member_type", res.data.member_type);
            wx.setStorageSync("phone", res.data.phone);
            wx.setStorageSync("staff_type", res.data.staff_type);
            this.$toast({
              content: "登录成功!"
            });
            wx.navigateBack({});
          } else {
            this.$toast({
              content: res.msg
            });
          }
        })
        .catch(err => {});
    },
    // 请求解析手机号登录接口
    getLogin() {
      wx.login({
        success: res => {
          if (res.code) {
            this.$ajax("/home/login/wxLogin", "post", {
              code: res.code
            })
              .then(res => {
                if (res.status) {
                  wx.setStorageSync("session_key", res.session_key); //获取到session_key,存入缓存(用于获取手机号)
                  this.postPhone();
                } else {
                }
              })
              .catch(err => {});
          }
        },
        fail() {
          wx.showLoading({
            title: "登录接口失败",
            mask: true
          });
        }
      });
    },
    // 请求接口解析手机号
    postPhone() {
      this.$ajax("/home/login/getPhone", "post", {
        nickName: wx.getStorageSync("nickName"),
        sessionKey: wx.getStorageSync("session_key"),
        encryptedData: this.encryptedData,
        iv: this.iv
      })
        .then(res => {
          if (res.status) {
            wx.setStorageSync("member_type", 2);
            wx.setStorageSync("phone", res.data);
            wx.navigateBack({});
          } else {
          }
        })
        .catch(err => {
        });
    },
    // 获取加密手机号
    getPhoneNumber(e) {
      wx.login({
        success: res => {
          if (res.code) {
            this.$ajax("/home/login/wxLogin", "post", {
              code: res.code
            })
              .then(res => {
                if (res.status) {
                } else {
                }
              })
              .catch(err => {});
          }
        },
        fail() {
          wx.showLoading({
            title: "登录接口失败",
            mask: true
          });
        }
      });
      this.encryptedData = e.mp.detail.encryptedData;
      this.iv = e.mp.detail.iv;
      if (e.mp.detail.errMsg == "getPhoneNumber:ok") {
        this.getLogin();
      } else {
        this.$toast({
          content: "拒绝授权"
        });
      }
    },
    // 判断手机号
    isPhone(phone) {
      return !(!/^(1[3-9])\d{9}$/.test(phone) || phone == "");
    },
    // 点击获取验证码
    getYzm() {
      if (this.isPhone(this.phone)) {
        this.$ajax("/home/GetInfo/getSmsCode", "post", {
          phone: this.phone,
          mark: "loginPhone"
        })
          .then(res => {
            console.log(res);
            if (res.status) {
              this.$toast({
                content: "已发送验证码"
              });
              this.timeDown();
            } else {
              this.$toast({
                content: res.msg
              });
            }
          })
          .catch(err => {});
      } else
        this.$toast({
          content: "请输入有效的手机号"
        });
    },
    //验证码60S倒计时
    timeDown() {
      this.yzmShow = false;
      this.timeClear = setInterval(() => {
        if (this.countdown === 0) {
          this.countdown = 60;
          clearInterval(this.timeClear);
          this.timeClear = null;
          this.yzmShow = true;
        } else {
          this.countdown--;
          // 发送请求获得验证码
        }
      }, 1000);
    },
    // 提交表单内容
    submit(e) {
      // let form_id = e.mp.detail.formId;
      if (!this.isPhone(this.phone))
        return this.$toast({
          content: "请输入有效手机号码"
        });
      if (this.yzm.length !== 6)
        return this.$toast({
          content: "请输入有效验证码"
        });
      this.loginIndex();
    },
  },
  onShow() {},
  onLoad(e) {
    Object.assign(this.$data, this.$options.data());
    this.member_type = e.member_type;
    this.loginExit();
    this.bjurl = this.$bjurl;
    clearInterval(this.timeClear);
  }
};
</script>

<style lang="scss" scoped>
.win {
  .bj_box {
    position: relative;
    background-image: url($bjurl+"login_bj.png");
    background-size: 100% 100%;
    width: 750rpx;
    height: 417rpx;
    padding-top: 170rpx;
    box-sizing: border-box;
    p {
      text-align: center;
      font-size: 60rpx;
      color: #ffffff;
    }
  }
  .form_box {
    width: 750rpx;
    position: relative;
    display: flex;
    justify-content: center;
    .position_box {
      box-shadow: 0rpx 20rpx 37rpx 13rpx rgba(4, 192, 211, 0.1);
      border-radius: 10rpx;
      position: absolute;
      top: -45rpx;
      background-color: #ffffff;
      left: 0;
      right: 0;
      width: 650rpx;
      margin: 0 auto;
      padding-bottom: 145rpx;
      box-sizing: border-box;
      .title_top {
        padding: 50rpx 0 20rpx 50rpx;
        color: #3e4a59;
        font-size: 48rpx;
      }
    }
  }
  .input_box {
    box-sizing: border-box;
    width: 590rpx;
    margin: 0 auto;
    input {
      height: 70rpx;
      font-size: 30rpx;
      color: #4d4d4d;
    }
    .code_row {
      justify-content: space-between;
    }
    .input_img_box {
      border-bottom: 1rpx solid #d3dfef;
      display: flex;
      align-items: center;
      margin: 6% 0 0 0;
      height: 100rpx;
      box-sizing: border-box;
      .phone_logo {
        width: 22rpx;
        height: 36rpx;
        margin-left: 50rpx;
      }
      .code_logo {
        width: 28rpx;
        height: 40rpx;
        margin-left: 47rpx;
      }
      .phone_input {
        margin-left: 45rpx;
      }
      .yzm_box {
        display: flex;
        align-items: center;
        .yzm {
          width: 260rpx;
          margin-left: 43rpx;
        }
      }
      button {
        border: 0;
        background-color: transparent;
        margin-right: 26rpx;
        width: 150rpx;
        height: 60rpx;
        line-height: 60rpx;
        color: $green;
        border-radius: 20rpx;
        font-size: 28rpx;
        text-decoration: underline;
      }
      .grey {
        color: #c3c3c3;
        text-decoration: #ffffff;
      }
    }
  }
  .btn_pos {
    position: relative;
    width: 650rpx;
    .btn_box {
      position: absolute;
      top: 108rpx;
      left: 0;
      right: 0;
      margin: 0 auto;
      .login_btn {
        line-height: 80rpx;
        margin: 0 auto;
        letter-spacing: 3rpx;
        color: #ffffff;
        font-size: 40rpx;
        width: 479rpx;
        height: 80rpx;
        background: linear-gradient(
          0deg,
          rgba(11, 208, 94, 1),
          rgba(11, 208, 173, 1)
        );
        border-radius: 46rpx;
      }
      .btn_row {
        margin-top: 42rpx;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 28rpx;
        color: #a2a2a2;
        .getPhone {
          background-color: transparent;
          font-size: 28rpx;
          border: 0;
          color: $green;
        }
      }
    }
  }
}
.container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  z-index: 99;
  .icon_load {
    margin-top: -12vh;
    p {
      text-align: center;
      font-size: 50rpx;
      color: #4d4d4d;
    }
  }
}
</style>
